import { handleResponse, Result } from "../util/handler";

type CategoryIdToName = { [categoryId: string]: string };

async function queryCategoryName(categoryId: number): Promise<Result<CategoryIdToName>> {
  const params = new URLSearchParams({ categoryId: ''+categoryId });
  return await fetch(`/api/category/getCategoryName?${params}`, {
    method: 'get',
    credentials: 'same-origin',
  }).then((resp) => handleResponse<CategoryIdToName>(resp));
}

export interface Category {
  categoryId: number,
  parentId: Category['categoryId'],
  categoryLevel: number,
  categoryName: string,
  list: Category[] | null
}

async function getCategories(): Promise<Result<Category[]>> {
  return await fetch('/api/category/getCategories', {
    method: 'get',
    credentials: 'same-origin',
  }).then((resp) => handleResponse<Category[]>(resp));
}

export {
  queryCategoryName,

  getCategories
}
